package cn.iocoder.yudao.module.pplan.service.specialmeal;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.pplan.controller.admin.specialmeal.vo.*;
import cn.iocoder.yudao.module.pplan.dal.dataobject.specialmeal.SpecialMealDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.pplan.dal.mysql.specialmeal.SpecialMealMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.pplan.enums.ErrorCodeConstants.*;

/**
 * 生产计划-特殊餐管理 Service 实现类
 *
 * @author yisl
 */
@Service
@Validated
public class SpecialMealServiceImpl implements SpecialMealService {

    @Resource
    private SpecialMealMapper specialMealMapper;

    @Override
    public Long createSpecialMeal(SpecialMealSaveReqVO createReqVO) {
        // 校验存在
        if (createReqVO.getMealCode() != null) {
            validateSpecialMealExists(createReqVO.getMealCode());
        }

        // 插入
        SpecialMealDO specialMeal = BeanUtils.toBean(createReqVO, SpecialMealDO.class);
        specialMealMapper.insert(specialMeal);
        // 返回
        return specialMeal.getId();
    }

    @Override
    public void updateSpecialMeal(SpecialMealSaveReqVO updateReqVO) {

        // 校验存在
        validateSpecialMealExists(updateReqVO.getId());
        // 校验存在
        if (updateReqVO.getMealCode() != null) {
            validateSpecialMealUpdateExists(updateReqVO.getId(), updateReqVO.getMealCode());
        }
        // 更新
        SpecialMealDO updateObj = BeanUtils.toBean(updateReqVO, SpecialMealDO.class);
        specialMealMapper.updateById(updateObj);
    }

    @Override
    public void deleteSpecialMeal(Long id) {
        // 校验存在
        validateSpecialMealExists(id);
        // 删除
        specialMealMapper.deleteById(id, false);
    }

    @Override
        public void deleteSpecialMealListByIds(List<Long> ids) {
        // 校验存在
        validateSpecialMealExists(ids);
        // 删除
        specialMealMapper.deleteByIds(ids,false);
        }

    private void validateSpecialMealExists(List<Long> ids) {
        List<SpecialMealDO> list = specialMealMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(SPECIAL_MEAL_NOT_EXISTS);
        }
    }

    private void validateSpecialMealExists(Long id) {
        if (specialMealMapper.selectById(id) == null) {
            throw exception(SPECIAL_MEAL_NOT_EXISTS);
        }
    }

    private void validateSpecialMealExists(String mealCode) {
        if (specialMealMapper.selectCount(SpecialMealDO::getMealCode, mealCode) != 0) {
            throw exception(SPECIAL_MEAL_CODE_EXISTS);
        }
    }

    private void validateSpecialMealUpdateExists(Long id, String mealCode) {
        SpecialMealDO specialMealDO = specialMealMapper.selectOne(SpecialMealDO::getMealCode, mealCode);
        if (ObjectUtil.isNotEmpty(specialMealDO) && !id.equals(specialMealDO.getId())) {
            throw exception(SPECIAL_MEAL_CODE_EXISTS);
        }
    }

    @Override
    public SpecialMealDO getSpecialMeal(Long id) {
        return specialMealMapper.selectById(id);
    }

    @Override
    public PageResult<SpecialMealDO> getSpecialMealPage(SpecialMealPageReqVO pageReqVO) {
        return specialMealMapper.selectPage(pageReqVO);
    }

}